package com.gotloop.jdo.key;

import java.io.Serializable;
import java.util.StringTokenizer;

import javax.jdo.identity.ObjectIdentity;

import com.gotloop.jdo.Loop;
import com.gotloop.jdo.UserProfile;


/**
 * Vote primary key.
 * @author jibhaine
 *
 */
public class VotePK implements Serializable {

	/**
	 * Generated serial version UID.
	 */
	private static final long serialVersionUID = 8883159041384055137L;

	/**
	 * The user who voted.
	 */
	public ObjectIdentity userProfile;

	/**
	 * The loop he voted for (or against).
	 */
	public ObjectIdentity loop;

	/**
	 * Public empty constructor.
	 */
	public VotePK() {
	}        
	
	/**
	 * Public constructor.
	 * @param s String of the form "user.name::loop.url"
	 */
	public VotePK(String s) {
		StringTokenizer token = new StringTokenizer(s, "::");
        this.userProfile = new ObjectIdentity(UserProfile.class, token.nextToken());
        this.loop = new ObjectIdentity(Loop.class, token.nextToken());
    }

	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return userProfile.toString() + "::" + loop.toString();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		if (loop == null) {
			result = prime * result	+ 0;
		} else {
			result = prime * result	+ loop.hashCode();
		}
		if (userProfile == null) {
			result = prime * result	+ 0;
		} else {
			result = prime * result	+ userProfile.hashCode();
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj != null && (obj instanceof VotePK)) {
			VotePK otherPK = (VotePK) obj;
			return otherPK.loop.equals(this.loop) 
				&& otherPK.userProfile.equals(this.userProfile);
		}
		return false;
	}

}
